package com.calendar.api.service.impl;

import com.calendar.api.model.Event;
import com.calendar.api.model.User;
import com.calendar.api.model.dto.EventDTO;
import com.calendar.api.repository.EventRepository;
import com.calendar.api.repository.UserRepository;
import com.calendar.api.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityNotFoundException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class EventServiceImpl implements EventService {

    private final EventRepository eventRepository;
    private final UserRepository userRepository;

    @Autowired
    public EventServiceImpl(EventRepository eventRepository, UserRepository userRepository) {
        this.eventRepository = eventRepository;
        this.userRepository = userRepository;
    }

    @Override
    public List<Event> getEvents(LocalDate startDate, LocalDate endDate, Long userId) {
        User user = getUserById(userId);
        return eventRepository.findByDateBetweenAndUser(startDate, endDate, user);
    }

    @Override
    public Event getEvent(Long id, Long userId) {
        Event event = eventRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("事件不存在: " + id));
        
        // 验证事件所属用户
        if (!event.getUser().getId().equals(userId)) {
            throw new EntityNotFoundException("事件不存在: " + id);
        }
        
        return event;
    }

    @Override
    public Event createEvent(EventDTO eventDTO, Long userId) {
        User user = getUserById(userId);
        Event event = convertToEntity(eventDTO);
        event.setUser(user);
        return eventRepository.save(event);
    }

    @Override
    public Event updateEvent(Long id, EventDTO eventDTO, Long userId) {
        Event existingEvent = getEvent(id, userId);
        
        existingEvent.setTitle(eventDTO.getTitle());
        existingEvent.setDate(LocalDate.parse(eventDTO.getDate()));
        existingEvent.setStartTime(eventDTO.getStartTime());
        existingEvent.setEndTime(eventDTO.getEndTime());
        existingEvent.setDescription(eventDTO.getDescription());
        
        return eventRepository.save(existingEvent);
    }

    @Override
    public void deleteEvent(Long id, Long userId) {
        Event event = getEvent(id, userId);
        eventRepository.delete(event);
    }
    
    private Event convertToEntity(EventDTO eventDTO) {
        Event event = new Event();
        event.setTitle(eventDTO.getTitle());
        event.setDate(LocalDate.parse(eventDTO.getDate()));
        event.setStartTime(eventDTO.getStartTime());
        event.setEndTime(eventDTO.getEndTime());
        event.setDescription(eventDTO.getDescription());
        return event;
    }
    
    private User getUserById(Long userId) {
        return userRepository.findById(userId)
                .orElseThrow(() -> new EntityNotFoundException("用户不存在: " + userId));
    }
} 